import requests
from bs4 import BeautifulSoup
import csv
import re
import time
import base64
import io
import hashlib
import hmac
from urllib.parse import urljoin, quote
import os
from datetime import datetime
from PIL import Image
# import email.utils  <- 不再需要

class ProductScraper:
    def __init__(self):
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        # Cloudflare R2 配置
        self.cloudflare_account_id = "2283f4ab71a58346bccac78dc6e5213e"
        self.r2_endpoint = f"https://{self.cloudflare_account_id}.r2.cloudflarestorage.com"
        
        # 从环境变量获取R2凭证
        self.r2_access_key_id = os.getenv('R2_ACCESS_KEY_ID')
        self.r2_secret_access_key = os.getenv('R2_SECRET_ACCESS_KEY')
        self.r2_bucket_name = "successurl"  # 使用 successurl 存储桶
        self.r2_region = "auto" # R2 通常使用 "auto" 区域
        
        print(f"R2 Access Key ID: {self.r2_access_key_id[:10]}..." if self.r2_access_key_id else "R2 Access Key ID: Not set")
        print(f"R2 Secret Access Key: {self.r2_secret_access_key[:10]}..." if self.r2_secret_access_key else "R2 Secret Access Key: Not set")
    
    # --- AWS SigV4 辅助函数 ---
    def sign(self, key, msg):
        """使用HMAC-SHA256签名"""
        return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()

    def get_signature_key(self, key, date_stamp, region_name, service_name):
        """获取AWS SigV4签名密钥"""
        k_date = self.sign(('AWS4' + key).encode('utf-8'), date_stamp)
        k_region = self.sign(k_date, region_name)
        k_service = self.sign(k_region, service_name)
        k_signing = self.sign(k_service, 'aws4_request')
        return k_signing
    # --- 结束辅助函数 ---
    
    # get_rfc_7231_date 已被移除，我们将使用 datetime.utcnow()
    
    def calculate_sha256(self, data):
        """计算内容的SHA256哈希"""
        if isinstance(data, str):
            data = data.encode('utf-8')
        return hashlib.sha256(data).hexdigest()
    
    def upload_to_r2(self, image_data, filename):
        """
        使用正确的 AWS Signature V4 上传图片到Cloudflare R2
        """
        try:
            # --- AWS SigV4 认证流程 ---
            service = 's3'
            host = self.r2_endpoint.replace('https://', '')
            
            # 1. 获取当前UTC时间和日期戳
            t = datetime.utcnow()
            amz_date = t.strftime('%Y%m%dT%H%M%SZ') # ISO 8601 格式
            date_stamp = t.strftime('%Y%m%d') # YYYYMMDD 格式
            http_date = t.strftime('%a, %d %b %Y %H:%M:%S GMT') # RFC 7231 格式

            # 2. 准备请求
            encoded_filename = quote(filename)
            canonical_uri = f"/{self.r2_bucket_name}/{encoded_filename}"
            upload_url = f"{self.r2_endpoint}{canonical_uri}"
            
            content_sha256 = self.calculate_sha256(image_data)
            
            # 3. 创建规范请求 (Canonical Request)
            canonical_querystring = ''
            canonical_headers = (
                f"host:{host}\n"
                f"x-amz-acl:public-read\n"
                f"x-amz-content-sha256:{content_sha256}\n"
                f"x-amz-date:{amz_date}\n"
            )
            signed_headers = 'host;x-amz-acl;x-amz-content-sha256;x-amz-date'
            
            canonical_request = (
                f"PUT\n"
                f"{canonical_uri}\n"
                f"{canonical_querystring}\n"
                f"{canonical_headers}\n"
                f"{signed_headers}\n"
                f"{content_sha256}" # 载荷哈希
            )

            # 4. 创建待签名字符串 (String to Sign)
            algorithm = 'AWS4-HMAC-SHA256'
            credential_scope = f"{date_stamp}/{self.r2_region}/{service}/aws4_request"
            
            string_to_sign = (
                f"{algorithm}\n"
                f"{amz_date}\n"
                f"{credential_scope}\n"
                f"{hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()}"
            )

            # 5. 计算签名
            signing_key = self.get_signature_key(self.r2_secret_access_key, date_stamp, self.r2_region, service)
            signature = hmac.new(signing_key, string_to_sign.encode('utf-8'), hashlib.sha256).hexdigest()

            # 6. 构建 Authorization 头部
            authorization_header = (
                f"{algorithm} Credential={self.r2_access_key_id}/{credential_scope}, "
                f"SignedHeaders={signed_headers}, Signature={signature}"
            )
            
            # 7. 组装最终的头部
            headers = {
                'Content-Type': 'image/jpeg',
                'Content-Length': str(len(image_data)),
                'Date': http_date, # RFC 7231 日期
                'x-amz-acl': 'public-read',
                'x-amz-content-sha256': content_sha256,
                'x-amz-date': amz_date, # ISO 8601 日期
                'Authorization': authorization_header
            }
            # --- 认证流程结束 ---

            print(f"🔄 正在上传图片到R2: {filename} ({len(image_data)} bytes)")
            print(f"x-amz-date 头: {amz_date}")
            
            # 上传图片
            response = requests.put(upload_url, data=image_data, headers=headers, timeout=30)
            
            if response.status_code in [200, 201]:
                # 返回公共访问URL
                public_url = f"https://image.successon.org/{encoded_filename}"
                print(f"✅ 图片上传成功: {public_url}")
                return public_url
            else:
                print(f"❌ 图片上传失败: {response.status_code}")
                print(f"响应内容: {response.text}")
                return None
                
        except Exception as e:
            print(f"❌ 上传到R2时出错: {e}")
            import traceback
            traceback.print_exc()
            return None

    # 其余代码保持不变...
    def convert_image_to_jpg(self, image_url):
        """
        下载图片并转换为JPG格式，返回JPG图片的二进制数据
        """
        try:
            print(f"🔄 正在转换图片: {image_url}")
            
            # 下载图片
            response = requests.get(image_url, headers=self.headers, timeout=30)
            response.raise_for_status()
            
            # 打开图片
            image = Image.open(io.BytesIO(response.content))
            
            # 转换为RGB模式（确保兼容JPG）
            if image.mode in ('RGBA', 'P', 'LA'):
                # 创建白色背景
                background = Image.new('RGB', image.size, (255, 255, 255))
                if image.mode == 'RGBA':
                    background.paste(image, mask=image.split()[-1])
                else:
                    background.paste(image)
                image = background
            elif image.mode != 'RGB':
                image = image.convert('RGB')
            
            # 将图片保存为JPG格式到内存中
            output_buffer = io.BytesIO()
            image.save(output_buffer, format='JPEG', quality=85, optimize=True)
            jpg_data = output_buffer.getvalue()
            
            print(f"✅ 图片转换成功: {image_url} -> JPG ({len(jpg_data)} bytes)")
            return jpg_data
            
        except Exception as e:
            print(f"❌ 图片转换失败 {image_url}: {e}")
            return None
    
    def process_image_url(self, image_url, product_name, image_index):
        """
        处理图片URL：如果是非JPG格式则转换并上传到R2
        """
        # 检查图片格式
        parsed_url = image_url.lower().split('?')[0]
        image_ext = parsed_url.split('.')[-1] if '.' in parsed_url else ''
        
        # 如果是JPG/JPEG/PNG/GIF/WEBP格式，直接使用原URL
        if image_ext in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
            print(f"✅ 使用原图片URL: {image_url}")
            return image_url
        
        # 如果是AVIF或其他格式，转换为JPG并上传到R2
        print(f"🔄 检测到 {image_ext.upper()} 格式，开始转换...")
        jpg_data = self.convert_image_to_jpg(image_url)
        
        if jpg_data:
            # 生成安全的文件名
            safe_name = re.sub(r'[^a-zA-Z0-9]', '_', product_name)[:30]
            timestamp = int(time.time())
            filename = f"{safe_name}_{image_index}_{timestamp}.jpg"
            
            # 上传到R2
            r2_url = self.upload_to_r2(jpg_data, filename)
            if r2_url:
                return r2_url
            else:
                print(f"⚠️ R2上传失败，尝试直接使用转换后的base64数据")
                # 如果上传失败，可以返回base64数据（但CSV可能不支持很长的base64字符串）
                base64_data = base64.b64encode(jpg_data).decode('utf-8')
                return f"data:image/jpeg;base64,{base64_data}"
        
        # 如果转换失败，返回原URL
        print(f"⚠️ 转换失败，使用原URL: {image_url}")
        return image_url
    
    def extract_background_image_url(self, style_attr):
        """
        从background-image样式中提取图片URL
        """
        if not style_attr:
            return None
        
        match = re.search(r'url\(["\']?(.*?)["\']?\)', style_attr)
        if match:
            url = match.group(1).strip('"\'')
            return url
        return None
    
    def make_url_absolute(self, url):
        """
        将相对URL转换为绝对URL
        """
        if not url:
            return None
        
        if url.startswith('//'):
            return 'https:' + url
        elif url.startswith('/'):
            return 'https://www.theluxedeals.com' + url
        else:
            return url
    
    def extract_product_data(self, html_content, product_url):
        """
        从商品页面HTML中提取数据
        """
        soup = BeautifulSoup(html_content, 'html.parser')
        
        # 使用可导入CSV的确切字段名
        product_data = {
            'Product ID': '',
            'Active (0/1)': '1',
            'Name *': '',
            'Categories (x,y,z...)': '',
            'Price tax excluded': '',
            'Tax rules ID': '1',
            'Wholesale price': '',
            'On sale (0/1)': '0',
            'Discount amount': '',
            'Discount percent': '',
            'Discount from (yyyy-mm-dd)': '',
            'Discount to (yyyy-mm-dd)': '',
            'Reference #': '',
            'Supplier reference #': '',
            'Supplier': 'Fashion Supplier',
            'Manufacturer': '',
            'EAN13': '',
            'UPC': '',
            'MPN': '',
            'Ecotax': '0.00',
            'Width': '0.000000',
            'Height': '0.000000',
            'Depth': '0.000000',
            'Weight': '0.300000',
            'Delivery time of in-stock products': '24-48 hours',
            'Delivery time of out-of-stock products with allowed orders': '3-5 days',
            'Quantity': '1000',
            'Minimal quantity': '1',
            'Low stock level': '5',
            'Receive a low stock alert by email': '0',
            'Stock location': '',
            'Visibility': 'both',
            'Additional shipping cost': '0.00',
            'Unity': '',
            'Unit price': '',
            'Summary': '',
            'Description': '',
            'Tags (x,y,z...)': '',
            'Meta title': '',
            'Meta description': '',
            'URL rewritten': '',
            'Text when in stock': 'In Stock',
            'Text when backorder allowed': 'Current supply. Ordering available',
            'Available for order (0 = No, 1 = Yes)': '1',
            'Product available date': '',
            'Product creation date': '',
            'Show price (0 = No, 1 = Yes)': '1',
            'Image URLs (x,y,z...)': '',
            'Image alt texts (x,y,z...)': '',
            'Delete existing images (0 = No, 1 = Yes)': '0',
            'Feature(Name:Value:Position)': '',
            'Available online only (0 = No, 1 = Yes)': '0',
            'Condition': 'new',
            'Customizable (0 = No, 1 = Yes)': '0',
            'Uploadable files (0 = No, 1 = Yes)': '0',
            'Text fields (0 = No, 1 = Yes)': '0',
            'Out of stock action': '2',
            'Virtual product': '0',
            'File URL': '',
            'Number of allowed downloads': '0',
            'Expiration date': '',
            'Number of days': '0',
            'ID / Name of shop': 'default',
            'Advanced stock management': '0',
            'Depends On Stock': '0',
            'Warehouse': '',
            'Acessories  (x,y,z...)': ''
        }
        
        try:
            # 提取产品名称
            title_element = soup.find('h1', class_='product_title')
            if title_element:
                product_name = title_element.get_text(strip=True)
                product_data['Name *'] = product_name
                product_data['Meta title'] = product_name
                
                # 生成友好URL
                friendly_url = re.sub(r'[^a-zA-Z0-9\s-]', '', product_name)
                friendly_url = re.sub(r'\s+', '-', friendly_url).lower()
                product_data['URL rewritten'] = friendly_url
            
            # 提取价格
            price_element = soup.find('p', class_='price')
            if price_element:
                price_text = price_element.get_text(strip=True)
                price_match = re.search(r'\$([\d.]+)', price_text)
                if price_match:
                    price = price_match.group(1)
                    product_data['Price tax excluded'] = price
                    
                    # 批发价格设置为价格的60%
                    try:
                        wholesale_price = float(price) * 0.6
                        product_data['Wholesale price'] = f"{wholesale_price:.2f}"
                    except:
                        product_data['Wholesale price'] = price
            
            # 提取制造商/品牌
            if 'CH@NEL' in product_data['Name *'] or 'CHANEL' in product_data['Name *']:
                product_data['Manufacturer'] = 'Chanel'
            elif 'LOUIS VUITTON' in product_data['Name *']:
                product_data['Manufacturer'] = 'Louis Vuitton'
            elif 'MIU MIU' in product_data['Name *']:
                product_data['Manufacturer'] = 'Miu Miu'
            elif 'THE ROW' in product_data['Name *']:
                product_data['Manufacturer'] = 'The Row'
            elif 'FENDI' in product_data['Name *']:
                product_data['Manufacturer'] = 'Fendi'
            elif 'PRADA' in product_data['Name *']:
                product_data['Manufacturer'] = 'Prada'
            elif 'GUCCI' in product_data['Name *']:
                product_data['Manufacturer'] = 'Gucci'
            elif 'HERMES' in product_data['Name *']:
                product_data['Manufacturer'] = 'Hermes'
            elif 'DIOR' in product_data['Name *']:
                product_data['Manufacturer'] = 'Dior'
            elif 'BOTTEGA VENETA' in product_data['Name *']:
                product_data['Manufacturer'] = 'Bottega Veneta'
            elif 'BALENCIAGA' in product_data['Name *']:
                product_data['Manufacturer'] = 'Balenciaga'
            else:
                product_data['Manufacturer'] = 'Designer Brand'
            
            # 提取描述
            description_element = soup.find('div', class_='elementor-widget-wd_single_product_content')
            if description_element:
                description_text = description_element.get_text(strip=True)
                # 清理描述文本
                description_text = re.sub(r'\s+', ' ', description_text)
                
                product_data['Description'] = f"<p>{description_text}</p>"
                
                # 摘要取前200个字符
                summary = description_text[:200] + '...' if len(description_text) > 200 else description_text
                product_data['Summary'] = f"<p>{summary}</p>"
                product_data['Meta description'] = summary
            
            # 提取分类
            categories_element = soup.find('span', class_='posted_in')
            if categories_element:
                categories = []
                category_links = categories_element.find_all('a', rel='tag')
                for link in category_links:
                    category_name = link.get_text(strip=True)
                    categories.append(category_name)
                
                if categories:
                    product_data['Categories (x,y,z...)'] = ','.join(categories)
            
            # 提取标签
            tags = []
            breadcrumb = soup.find('nav', class_='wd-breadcrumbs')
            if breadcrumb:
                breadcrumb_links = breadcrumb.find_all('a')
                for link in breadcrumb_links:
                    tag = link.get_text(strip=True)
                    if tag and tag.lower() != 'home':
                        tags.append(tag)
            
            # 从分类中提取额外标签
            if categories_element:
                category_links = categories_element.find_all('a', rel='tag')
                for link in category_links:
                    tag = link.get_text(strip=True)
                    if tag not in tags:
                        tags.append(tag)
            
            # 从产品名称中提取关键词作为标签
            if product_data['Name *']:
                name_keywords = re.findall(r'[A-Z][a-z]+|[A-Z]+', product_data['Name *'])
                for keyword in name_keywords:
                    if len(keyword) > 2 and keyword not in tags:
                        tags.append(keyword)
            
            if tags:
                product_data['Tags (x,y,z...)'] = ','.join(tags)
            
            # 提取图片
            image_urls = []
            image_alt_texts = []
            
            print("正在提取图片URL...")
            
            # 从背景图片中提取
            photo_elements = soup.find_all('div', class_='wcgs-photo')
            for photo in photo_elements:
                style_attr = photo.get('style', '')
                bg_url = self.extract_background_image_url(style_attr)
                if bg_url:
                    absolute_url = self.make_url_absolute(bg_url)
                    if absolute_url and absolute_url not in image_urls:
                        image_urls.append(absolute_url)
                        image_alt_texts.append(product_data['Name *'])
                        print(f"✅ 找到背景图片: {absolute_url}")
            
            # 从所有img标签中提取
            all_images = soup.find_all('img')
            for img in all_images:
                img_src = img.get('src')
                if img_src and not img_src.startswith('data:image/'):
                    absolute_url = self.make_url_absolute(img_src)
                    if absolute_url and absolute_url not in image_urls:
                        img_alt = img.get('alt', product_data['Name *'])
                        image_urls.append(absolute_url)
                        image_alt_texts.append(img_alt)
                        print(f"✅ 找到图片: {absolute_url}")
            
            # 从data-image属性中提取
            for img in all_images:
                data_image = img.get('data-image')
                if data_image and not data_image.startswith('data:image/'):
                    absolute_url = self.make_url_absolute(data_image)
                    if absolute_url and absolute_url not in image_urls:
                        img_alt = img.get('alt', product_data['Name *'])
                        image_urls.append(absolute_url)
                        image_alt_texts.append(img_alt)
                        print(f"✅ 找到data-image图片: {absolute_url}")
            
            # 处理图片格式转换
            processed_image_urls = []
            processed_alt_texts = []
            
            for i, (img_url, alt_text) in enumerate(zip(image_urls, image_alt_texts)):
                print(f"\n🔄 处理第 {i+1}/{len(image_urls)} 张图片...")
                processed_url = self.process_image_url(img_url, product_data['Name *'], i)
                processed_image_urls.append(processed_url)
                processed_alt_texts.append(alt_text)
            
            # 去重
            unique_image_urls = []
            unique_alt_texts = []
            seen_urls = set()
            
            for url, alt in zip(processed_image_urls, processed_alt_texts):
                if url and url not in seen_urls:
                    seen_urls.add(url)
                    unique_image_urls.append(url)
                    unique_alt_texts.append(alt)
            
            if unique_image_urls:
                product_data['Image URLs (x,y,z...)'] = ','.join(unique_image_urls)
                product_data['Image alt texts (x,y,z...)'] = ','.join(unique_alt_texts)
                print(f"🎉 成功处理 {len(unique_image_urls)} 张图片")
            else:
                print("❌ 未找到图片")
            
            # 生成参考号
            if product_data['Name *']:
                ref_base = re.sub(r'[^a-zA-Z0-9]', '', product_data['Name *'])[:8].upper()
                product_data['Reference #'] = f"{ref_base}001"
                product_data['Supplier reference #'] = product_data['Reference #']
            
            # 提取特征
            features = []
            
            # 根据产品类型添加特征
            if 'Boots' in product_data.get('Categories (x,y,z...)', ''):
                features.append(f"Type:Boots:0")
                features.append(f"Material:Leather:1")
                features.append(f"Heel Height:High:2")
            elif 'Sneakers' in product_data.get('Categories (x,y,z...)', ''):
                features.append(f"Type:Sneakers:0")
                features.append(f"Material:Leather:1")
                features.append(f"Sole:Rubber:2")
            elif 'Heels' in product_data.get('Categories (x,y,z...)', ''):
                features.append(f"Type:Heels:0")
                features.append(f"Material:Leather:1")
                features.append(f"Heel Height:High:2")
            elif 'Handbag' in product_data.get('Name *', '') or 'Bag' in product_data.get('Name *', ''):
                features.append(f"Type:Handbag:0")
                features.append(f"Material:Leather:1")
                features.append(f"Closure:Zip:2")
            
            # 通用特征
            features.append(f"Style:Designer:3")
            features.append(f"Quality:High:4")
            
            if features:
                product_data['Feature(Name:Value:Position)'] = '|'.join(features)
            
            # 设置产品创建日期
            current_date = datetime.now().strftime('%Y-%m-%d')
            product_data['Product creation date'] = current_date
            product_data['Product available date'] = current_date
            
            # 设置EAN13
            product_data['EAN13'] = '1234567890123'
            
        except Exception as e:
            print(f"提取数据时出错: {e}")
            import traceback
            traceback.print_exc()
        
        return product_data
    
    def scrape_product_page(self, product_url):
        """
        爬取单个商品页面
        """
        try:
            print(f"正在访问: {product_url}")
            response = requests.get(product_url, headers=self.headers)
            response.raise_for_status()
            
            product_data = self.extract_product_data(response.text, product_url)
            return product_data
            
        except requests.RequestException as e:
            print(f"请求商品页面失败: {e}")
            return None
    
    def save_to_csv(self, products_data, filename='luxury_products_import.csv'):
        """
        将产品数据保存为CSV文件
        """
        if not products_data:
            print("没有数据可保存")
            return
        
        # 使用可导入CSV的确切字段名
        fieldnames = [
            'Product ID', 'Active (0/1)', 'Name *', 'Categories (x,y,z...)', 
            'Price tax excluded', 'Tax rules ID', 'Wholesale price', 
            'On sale (0/1)', 'Discount amount', 'Discount percent', 
            'Discount from (yyyy-mm-dd)', 'Discount to (yyyy-mm-dd)', 
            'Reference #', 'Supplier reference #', 'Supplier', 'Manufacturer', 
            'EAN13', 'UPC', 'MPN', 'Ecotax', 'Width', 'Height', 'Depth', 
            'Weight', 'Delivery time of in-stock products', 
            'Delivery time of out-of-stock products with allowed orders', 
            'Quantity', 'Minimal quantity', 'Low stock level', 
            'Receive a low stock alert by email', 'Stock location', 
            'Visibility', 'Additional shipping cost', 'Unity', 'Unit price', 
            'Summary', 'Description', 'Tags (x,y,z...)', 'Meta title', 
            'Meta description', 'URL rewritten', 'Text when in stock', 
            'Text when backorder allowed', 'Available for order (0 = No, 1 = Yes)', 
            'Product available date', 'Product creation date', 
            'Show price (0 = No, 1 = Yes)', 'Image URLs (x,y,z...)', 
            'Image alt texts (x,y,z...)', 'Delete existing images (0 = No, 1 = Yes)', 
            'Feature(Name:Value:Position)', 'Available online only (0 = No, 1 = Yes)', 
            'Condition', 'Customizable (0 = No, 1 = Yes)', 
            'Uploadable files (0 = No, 1 = Yes)', 'Text fields (0 = No, 1 = Yes)', 
            'Out of stock action', 'Virtual product', 'File URL', 
            'Number of allowed downloads', 'Expiration date', 'Number of days', 
            'ID / Name of shop', 'Advanced stock management', 'Depends On Stock', 
            'Warehouse', 'Acessories  (x,y,z...)'
        ]
        
        with open(filename, 'w', newline='', encoding='utf-8') as csvfile:
            # 使用分号作为分隔符，这是欧洲CSV的常见格式
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter=';', quoting=csv.QUOTE_ALL)
            writer.writeheader()
            
            for i, product in enumerate(products_data, 1):
                product['Product ID'] = str(i)
                writer.writerow(product)
        
        print(f"数据已保存到 {filename}，共 {len(products_data)} 个产品")

def main():
    scraper = ProductScraper()
    
    # 检查R2凭证
    if not scraper.r2_access_key_id or not scraper.r2_secret_access_key:
        print("❌ 请设置R2访问密钥环境变量：")
        print("export R2_ACCESS_KEY_ID=your_actual_access_key_id")
        print("export R2_SECRET_ACCESS_KEY=your_actual_secret_access_key")
        return
    
    # 从您提供的HTML内容中提取产品URL
    product_urls = []
    
    # 如果您有多个产品URL，可以在这里添加
    # 或者从文件读取
    try:
        with open('product_links.txt', 'r', encoding='utf-8') as f:
            product_urls = [line.strip() for line in f if line.strip()]
    except FileNotFoundError:
        print("未找到 product_links.txt 文件")
        
        # 使用示例URL
        product_urls = [
            "https://www.theluxedeals.com/product/louis-vuitton-keepall-bandouliere-25-m46271-251511cm/",
        ]
    
    print(f"开始处理 {len(product_urls)} 个商品...")
    
    all_products_data = []
    
    for i, url in enumerate(product_urls, 1):
        print(f"\n正在处理第 {i}/{len(product_urls)} 个商品")
        
        product_data = scraper.scrape_product_page(url)
        if product_data:
            all_products_data.append(product_data)
            print(f"✅ 成功提取: {product_data['Name *']}")
            
            # 显示图片信息
            image_count = len(product_data['Image URLs (x,y,z...)'].split(',')) if product_data['Image URLs (x,y,z...)'] else 0
            print(f"  价格: ${product_data['Price tax excluded']}")
            print(f"  图片数量: {image_count}")
            if image_count > 0:
                images = product_data['Image URLs (x,y,z...)'].split(',')
                for j, img_url in enumerate(images[:5]):  # 显示前5张图片
                    print(f"  图片 {j+1}: {img_url}")
        else:
            print(f"❌ 提取失败: {url}")
        
        # 添加延迟避免请求过于频繁
        time.sleep(2)
    
    # 保存为CSV
    if all_products_data:
        scraper.save_to_csv(all_products_data, 'luxury_products_import.csv')
        print(f"\n🎉 成功处理 {len(all_products_data)} 个产品！")
        
        # 显示一些统计信息
        print("\n导出统计:")
        for product in all_products_data:
            image_count = len(product['Image URLs (x,y,z...)'].split(',')) if product['Image URLs (x,y,z...)'] else 0
            print(f"- {product['Name *']}: ${product['Price tax excluded']} ({image_count} 张图片)")
            
    else:
        print("\n没有成功提取到任何产品数据")

if __name__ == "__main__":
    main()